import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

import 'package:test_performance/vm/test4_vm.dart';

/*
 * @Author: zhudaihao
 * @Date: 2022/3/11
 * @Describe:
 */

class Test4Provider extends StatefulWidget {
  const Test4Provider({Key? key}) : super(key: key);

  @override
  State<Test4Provider> createState() => _Test4ProviderState();
}

class _Test4ProviderState extends State<Test4Provider> {
  Test4VM test4vm = Test4VM();

  @override
  Widget build(BuildContext context) {
    debugPrint('-----build');
    return ChangeNotifierProvider(
      create: (context) => test4vm,
      child: Scaffold(
        appBar: AppBar(
          title: const Text("测试provider"),
        ),
        body: Column(
          children: [
            Container(),
            const Text("data"),
            //1
            ElevatedButton(
                onPressed: () {
                  test4vm.addNumber();
                },
                child: Text("${test4vm.add}")),

            //2
            ElevatedButton(
                onPressed: () {
                  test4vm.addNumber2();
                },
                child: Text("${test4vm.add2}")),

            // Container(
            //   padding: const EdgeInsets.only(top: 16, bottom: 16),
            //   child: Row(
            //     children: [
            //       const Text("默认选择",
            //           style: TextStyle(
            //               color: Colors.blue,
            //               fontSize: 28,
            //               height: 1.38,
            //               fontWeight: FontWeight.w500)),
            //       const Text(" *",
            //           style: TextStyle(color: Colors.redAccent, height: 1.3)),
            //       const Spacer(),
            //       Consumer(
            //         builder: (BuildContext context, value, Widget? child) {
            //           //
            //           return Consumer<Test4VM>(builder:
            //               (BuildContext context, value, Widget? child) {
            //             return CupertinoSwitch(
            //               activeColor: Colors.green,
            //               trackColor: Colors.grey,
            //               value: test4vm.isSelect,
            //               onChanged: (data) {
            //                 test4vm.setSelect(data);
            //               },
            //             );
            //           });
            //         },
            //       )
            //     ],
            //   ),
            // ),

            // TitleSwitchWidget("默认选择", child: Consumer(
            //   builder: (BuildContext context, value, Widget? child) {
            //     //
            //     debugPrint('zdh-----------------Selector');
            //     return Selector(
            //         builder: (BuildContext context, bool value, Widget? child) {
            //       return CupertinoSwitch(
            //         activeColor: Colors.red,
            //         trackColor: Colors.grey,
            //         value: value,
            //         onChanged: (data) {
            //           test4vm.setSelect(data);
            //         },
            //       );
            //     }, selector: (BuildContext context, Test4VM data) {
            //       return data.isSelect;
            //     });
            //     return Consumer<Test4VM>(
            //         builder: (BuildContext context, value, Widget? child) {
            //       return CupertinoSwitch(
            //         activeColor: Colors.red,
            //         trackColor: Colors.grey,
            //         value: test4vm.isSelect,
            //         onChanged: (data) {
            //           test4vm.setSelect(data);
            //         },
            //       );
            //     });
            //   },
            // )),

            // Consumer<Test4VM>(
            //     builder: (BuildContext context, value, Widget? child) {
            //   return CupertinoSwitch(
            //     activeColor: Colors.blue,
            //     trackColor: Colors.grey,
            //     value: test4vm.isSelect2,
            //     onChanged: (data) {
            //       test4vm.setSelect2(data);
            //     },
            //   );
            // }),

            // TestSwitch(
            //   test4vm.isSelect2,
            //   onChanged: (bool value) {
            //     test4vm.setSelect2(value);
            //   },
            // ),


            // Selector(
            //     builder: (BuildContext context, bool value, Widget? child) {
            //       return CupertinoSwitch(
            //         activeColor: Colors.green,
            //         trackColor: Colors.grey,
            //         value: test4vm.isSelect,
            //         onChanged: (data) {
            //           test4vm.setSelect(data);
            //         },
            //       );
            //     }, selector: (BuildContext context, Test4VM data) {
            //   return data.isSelect;
            // }),
            //
            //
            //
            // Selector(
            //     builder: (BuildContext context, bool value, Widget? child) {
            //       return CupertinoSwitch(
            //         activeColor: Colors.red,
            //         trackColor: Colors.grey,
            //         value: value,
            //         onChanged: (data) {
            //           test4vm.setSelect2(data);
            //         },
            //       );
            //     }, selector: (BuildContext context, Test4VM data) {
            //   return data.isSelect2;
            // }),

            TestSelector(test4vm),

            Expanded(
              child: ListView.builder(
                itemBuilder: (context, int index) {
                  return Container(
                    margin: const EdgeInsets.all(10),
                    width: 50,
                    height: 50,
                    color: Colors.red,
                  );
                },
                itemCount: 3,
              ),
            ),
          ],
        ),
      ),
    );
  }
}

class TestSelector extends StatelessWidget {
  final Test4VM test4vm;
  const TestSelector(this.test4vm,{Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Column(children: [
      Selector(
          builder: (BuildContext context, bool value, Widget? child) {
            return CupertinoSwitch(
              activeColor: Colors.green,
              trackColor: Colors.grey,
              value: test4vm.isSelect,
              onChanged: (data) {
                test4vm.setSelect(data);
              },
            );
          }, selector: (BuildContext context, Test4VM data) {
        return data.isSelect;
      }),



      Selector(
          builder: (BuildContext context, bool value, Widget? child) {
            return CupertinoSwitch(
              activeColor: Colors.red,
              trackColor: Colors.grey,
              value: value,
              onChanged: (data) {
                test4vm.setSelect2(data);
              },
            );
          }, selector: (BuildContext context, Test4VM data) {
        return data.isSelect2;
      }),
    ],);
  }
}



///局部刷新
class LocalRefactorSwitch extends StatefulWidget {
  ///初始化按钮状态
  final bool? value;
  final Function(bool value)? onChanged;

  const LocalRefactorSwitch(this.value, {Key? key, this.onChanged}) : super(key: key);

  @override
  _LocalRefactorSwitchState createState() => _LocalRefactorSwitchState();
}

class _LocalRefactorSwitchState extends State<LocalRefactorSwitch> {
  late bool value;

  @override
  void initState() {
    super.initState();
    value = widget.value ?? false;
  }

  @override
  Widget build(BuildContext context) {
    return CupertinoSwitch(
      activeColor: Colors.blue,
      trackColor: Colors.grey,
      value: value,
      onChanged: (data) {
        setState(() {
          value=data;
          widget.onChanged ?? (value);
        });
      },
    );
  }
}

///带标题开关按钮
class TitleSwitchWidget extends StatelessWidget {
  final String title;

  ///是否显示*
  final bool? visibleHint;
  final Widget? child;

  const TitleSwitchWidget(this.title, {Key? key, this.visibleHint, this.child})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: const EdgeInsets.only(top: 16, bottom: 16),
      child: Row(
        children: [
          Text(title,
              style: const TextStyle(
                  color: Colors.blue,
                  fontSize: 28,
                  height: 1.38,
                  fontWeight: FontWeight.w500)),
          Text(visibleHint ?? false ? " *" : "",
              style: const TextStyle(color: Colors.redAccent, height: 1.3)),
          const Spacer(),
          child ?? const SizedBox(),
        ],
      ),
    );
  }
}
